package com.spzx.controller;

import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.api.domain.VO.CategoryVO;
import com.spzx.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/category")
@Tag(name = "商品分类接口管理")
public class CategoryController extends BaseController {
    @Autowired
    CategoryService categoryService;

    @InnerAuth
    @GetMapping("/indexCategoryList")
    @Operation(summary = "内部调用接口:查询1级分类列表")
    public R<List<CategoryVO>> getFirstCategoryList() {
        return R.ok(categoryService.getFirstCategoryList(0L));
    }

    @InnerAuth
    @GetMapping("/tree")
    @Operation(summary = "内部调用:获取分类下拉树列表")
    public R<List<CategoryVO>> getCategoryTree() {
        return R.ok(categoryService.getCategoryTree());
    }

    @GetMapping("/list/{id}")
    @Operation(summary = "获取分类树下拉列表")
    public AjaxResult getTreeList(@PathVariable Long id) {
        return AjaxResult.success(categoryService.getListByFatherId(id));
    }

    @PostMapping("/import")
    @Operation(summary = "导入")
    public AjaxResult importCategory(@RequestParam MultipartFile file) {
        return toAjax(categoryService.importCategory(file));
    }

    @PostMapping("/export")
    @Operation(summary = "导出")
    public void exportCategory(HttpServletResponse response) {
        categoryService.exportCategory(response);
    }

    @PostMapping("/importTemplate")
    @Operation(summary = "下载导出模板")
    public void importTemplate(HttpServletResponse response) {
        categoryService.importTemplate(response);
    }

}
